package com.comum.javahomework420242025.controller;
import com.comum.javahomework420242025.pojo.*;
import com.comum.javahomework420242025.repository.DepartmentProductionRepository;
import com.comum.javahomework420242025.repository.ProductionRepository;
import com.comum.javahomework420242025.utils.MyFileUtil;
import com.comum.javahomework420242025.repository.AllFileRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@RestController
public class ProductionController {
    @Autowired
    private AllFileRepository allFileRepository;
    @Autowired
    private ProductionRepository productionRepository;
    @Autowired
    private DepartmentProductionRepository departmentProductionRepository;
    @PostMapping("/addProduction")
    public Result addProduction(@RequestBody Production production){
        production.setId(null);
        System.out.println(production);
        productionRepository.save(production);
        return Result.success();
    }
    @PutMapping("/updateProduction")
    public Result updateProduction(@RequestBody Production production){
        System.out.println(production);
        productionRepository.save(production);
        return Result.success();
    }
    @DeleteMapping("/deleteProduction")
    public Result deleteProduction(String id){
        System.out.println(id);
        List<DepartmentProduction> dp = departmentProductionRepository.findByProductionId(id);
        if(dp.size()!=0)return Result.error("该产品在价格本存在信息，无法删除！");
        productionRepository.deleteById(Long.valueOf(id));
        String type = "产品";
        allFileRepository.deleteByElseIdAndType(id,type);
        return Result.success();
    }
    @PostMapping("/getProduction")
    public Result<PageBean<Production>> getProduction(@RequestBody AskList askList){
        int pageSize = askList.getPageSize();
        int pageNum = askList.getPageNum();
        String str = askList.getStr();
        String type = askList.getFindType();
        PageBean<Production> pb = new PageBean<>();
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        Page<Production> page = productionRepository.selectByStr(str,type,pageable);
        pb.setTotal(page.getTotalElements());
        pb.setItems(page.getContent());
        System.out.println(pb);
        return Result.success(pb);
    }
    @GetMapping("/getproductionsforps")
    public Result<List<Production>>getproductionsforps(String type){
        List<Production> res = productionRepository.findAllByType(type);
        System.out.println(res);
        return Result.success(res);
    }
    @PostMapping("/uploadProductionFile") //产品管理上传文件
    public Result uploadProductionFile(String elseid,@RequestPart("files") MultipartFile[] files){
        for (MultipartFile file : files) {
            String fileName = MyFileUtil.uploadfile(file);
            AllFile allFile =new AllFile(null,Long.parseLong(elseid),"产品",fileName);
            allFileRepository.save(allFile);
        }
        return Result.success();
    }
    @GetMapping("/getLoadProductionFileName")
    public Result<List<String>>getloadfilename(String elseid){
        String type = "产品";
        List<String> res = allFileRepository.findfilenames(elseid,type);
        return Result.success(res);
    }

}
